<?
error_reporting(0);
require_once('bootstrap.php');
if (isset($_POST['db_settings_in_post'])) {
	// Test database connection
	$error = '';
	$link = mysql_connect($_POST['db_host'], $_POST['db_user'], $_POST['db_pass']);
	if (!$link) {
		$error = 'Kunne ikke forbinde til database serveren';
	} else {
		$db = mysql_select_db($_POST['db_name']);
		if (!$db) {
			$error = 'Kunne ikke forbinde til databasen';
		}
	}
	
	if ($error == '') {
		$_SESSION['db'] = $_POST;
		$goto_next_step = true;
	}
}

?>
<html>
	<head>
		<title>Installation af KomitCMS</title>
		<meta http-equiv="content-type" content="text/html;charset=utf-8" />
		<meta http-equiv="Content-Style-Type" content="text/css" />
		<link rel="stylesheet" type="text/css" href="../stylesheet/style.css" />
		<link rel="stylesheet" type="text/css" href="../stylesheet/install.css" />
		<script type="text/javascript">
			function fieldEmpty(jField) {
				return (jField.value == '');
			}
			function fieldConfirm(jField1, jField2) {
				return (jField1.value != jField2.value);
			}
			function setError(jField) {
				jField.style.border = '1px solid #FF0000';
			}
			function clearFields(jForm) {
				for(var i=0; i<jForm.elements.length; i++) {
					var jEl = jForm.elements[i];
					if (jEl.type == 'text' || jEl.type == 'password') {
						jEl.style.border = '';
					}
				}
			}
			function submitForm() {
				var jForm = document.forms[0];
				clearFields(jForm);
				var jErrors = Array();
				if (fieldEmpty(jForm['db_host'])) {
					jErrors.push('db_host');
				}
				if (fieldEmpty(jForm['db_user'])) {
					jErrors.push('db_user');
				}
				if (fieldEmpty(jForm['db_name'])) {
					jErrors.push('db_name');
				}
				if (jErrors.length > 0) {
					for (var i=0; i<jErrors.length; i++) {
						setError(jForm[jErrors[i]]);
					}
					return false;
				}
				jForm.submit();
				return true;
			}
			function nextStep() {
				submitForm();
			}
			function noenter() {
			  return !(window.event && window.event.keyCode == 13); 
			}
		</script>
	</head>
	
	<body style="margin: 0" <? if ($goto_next_step) { echo 'onload="parent.nextStep()"'; } ?>>
		<div id="control">
			<button onclick="parent.previousStep()">Forige</button>
			<button onclick="nextStep()">Næste</button>
		</div>
		<h2 class="page-header">Database</h2>
		<div id="description">
			Udfyld her dine database indstillinger. Disse er oftest blevet udleveret af din webhotel-udbyder. 'Table prefix' skal du kun ændre, hvis du har flere KomitCMS systemer installeret på samme database.
		</div>
		<div id="step" style="margin-top: 10px; padding-top: 10px; border-top: 1px solid #CCC">
			
			<? if ($error != '') { ?>
				<div style="margin: 10px 0px; padding: 10px; border: 1px solid #CCC; background: #EEE">
					<?= $error ?>
				</div>
			<? } ?>
			
			<form method="post">
				<input type="hidden" name="db_settings_in_post" value="1" />
				<table class="UI_Editor">
					<tr>
						<td class="label">Vært: </td>
						<td><input onkeypress="return noenter()" tabindex="1" type="text" name="db_host" value="<?=$_POST[db_host]?>"/></td>
					</tr>
					<tr>
						<td class="label">Brugernavn: </td>
						<td><input onkeypress="return noenter()" tabindex="2" type="text" name="db_user" value="<?=$_POST[db_user]?>"/></td>
					</tr>
					<tr>
						<td class="label">Password: </td>
						<td><input onkeypress="return noenter()" tabindex="3" type="password" name="db_pass" value="<?=$_POST[db_pass]?>"/></td>
					</tr>
					<tr>
						<td class="label">Database: </td>
						<td><input onkeypress="return noenter()" tabindex="4" type="text" name="db_name" value="<?=$_POST[db_name]?>"/></td>
					</tr>
					<tr>
						<td class="label">Tabel prefix: <br/>
							<small>(Brug 'cms' hvis du er i tvivl)</small></td>
						<td><input onkeypress="return noenter()" tabindex="5" type="text" name="db_prefix" value="<?=$_POST[db_prefix]?>"></td>
					</tr>
				</table>
			</form>				
		</div>
	</body>
</html>